/*
 * OptionsDialog.java
 *
 * Created on December 13, 2009, 7:07 PM
 */

package itemrecovertool;

import java.io.File;
import javax.swing.JOptionPane;

/**
 *
 * @author  Matt
 */
public class OptionsDialog extends javax.swing.JDialog {
    
    private MainFrame mainFrame;
    private boolean quitMode = false;
    
    /** Creates new form OptionsDialog */
    public OptionsDialog(MainFrame mainFrame) {
        super(mainFrame, true);
        this.mainFrame = mainFrame;
        initComponents();
        setLocationRelativeTo(mainFrame);
    }
    
    public void showOptions() {
        showOptions(false);
    }
    
    public void showOptions(boolean quitMode) {
        this.quitMode = quitMode;
        cancelBtn.setText(quitMode ? "Quit" : "Cancel");
        
        String dp = mainFrame.usingDefaultDataPath
                        ? mainFrame.defaultDataPath
                        : mainFrame.pathData;
        dataFolderField.setText(dp);
        executableFolderField.setText(mainFrame.pathExe);
        defaultBox.setSelected(mainFrame.usingDefaultDataPath);
        updateDefault();
        
        loadOnStartBox.setSelected(mainFrame.loadItemListOnStart);
        searchPathBox.setSelected(mainFrame.searchPaths);
        sortBox.setSelected(mainFrame.sortItemList);
        
        setVisible(true);    
    }
    
    private void updateDefault() {
        boolean b = !defaultBox.isSelected();
        dataFolderField.setEnabled(b);
        browseBtnData.setEnabled(b);
        if (defaultBox.isSelected()) 
            dataFolderField.setText(mainFrame.defaultDataPath);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        dataFolderField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        executableFolderField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        browseBtnExe = new javax.swing.JButton();
        browseBtnData = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        okBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        defaultBox = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        sortBox = new javax.swing.JCheckBox();
        searchPathBox = new javax.swing.JCheckBox();
        loadOnStartBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Options");

        jLabel1.setText("Torchlight Data Folder:");

        dataFolderField.setText("%appdata%/runic games/torchlight");
        dataFolderField.setEnabled(false);

        jLabel2.setText("Torchlight Executable:");

        executableFolderField.setText("C:/Program Files/Runic Games/Torchlight/Torchlight.exe");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("<html><b>Make sure these paths are correct!</b></html>");

        browseBtnExe.setText("...");

        browseBtnData.setText("...");
        browseBtnData.setEnabled(false);

        okBtn.setText("OK");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });
        jPanel1.add(okBtn);

        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });
        jPanel1.add(cancelBtn);

        defaultBox.setSelected(true);
        defaultBox.setText("Use Default");
        defaultBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultBoxActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Performance"));

        sortBox.setText("Sort Item List");

        searchPathBox.setText("Include DATFILE in searching");

        loadOnStartBox.setText("Load Item List On Startup");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(sortBox)
                    .add(searchPathBox)
                    .add(loadOnStartBox))
                .addContainerGap(148, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(sortBox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(searchPathBox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(loadOnStartBox)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 110, Short.MAX_VALUE)
                        .add(defaultBox))
                    .add(jLabel2)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                    .add(executableFolderField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                    .add(dataFolderField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(browseBtnData, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(browseBtnExe, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .add(11, 11, 11)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(defaultBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(browseBtnData)
                    .add(dataFolderField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(browseBtnExe)
                    .add(executableFolderField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void defaultBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultBoxActionPerformed
        updateDefault();
}//GEN-LAST:event_defaultBoxActionPerformed

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        boolean invalid = false;
        String data = null;
        String dataOld = mainFrame.pathData;
        boolean usingDefaultPath = true;
        
        String exe = executableFolderField.getText();
        if (exe.length()==0 || !new File(exe).exists())
            invalid = true;
        
        if (dataFolderField.getText().equalsIgnoreCase(mainFrame.defaultDataPath)) {
            defaultBox.setSelected(true);
            updateDefault();
        }
        
        //user wants to turn ON default setting
        if (defaultBox.isSelected()) {
            data = mainFrame.getDefaultDataPath();
            usingDefaultPath = true;
        }
        //user wants to turn OFF default setting
        else {
            data = dataFolderField.getText();
            usingDefaultPath = false;
        }
        
        //can't find the data path, don't allow to continue
        if (data==null || !new File(data).exists()) {
            String p = usingDefaultPath ? mainFrame.defaultDataPath : data;
            JOptionPane.showMessageDialog(this, "'"+p
                    +"' does not exist.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        } else if (!new File(data+"\\mods").exists()) {
            String p = usingDefaultPath ? mainFrame.defaultDataPath : data;
            JOptionPane.showMessageDialog(this, "'"+p
                    +"' does not point to the Torchlight data folder.", 
                    "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (invalid) {
            JOptionPane.showMessageDialog(this, "Can't find Torchlight.exe.", 
                    "Warning", JOptionPane.WARNING_MESSAGE);
        }
        mainFrame.setRunButtonEnabled(!invalid);
        mainFrame.pathExe = exe;
        mainFrame.pathData = data;
        mainFrame.usingDefaultDataPath = usingDefaultPath;
        System.out.println("default? "+usingDefaultPath);
        setVisible(false);
        
        mainFrame.searchPaths = searchPathBox.isSelected();
        mainFrame.loadItemListOnStart = loadOnStartBox.isSelected();
        
        
        boolean oldSort = mainFrame.sortItemList;    
        mainFrame.sortItemList = sortBox.isSelected();
        if (mainFrame.sortItemList!=oldSort && mainFrame.sortItemList)
            mainFrame.sortItemList();
        
        //update item list if the folder has changed
        if (mainFrame.items!=null && !data.equalsIgnoreCase(dataOld))
            mainFrame.populateItemList();
    }//GEN-LAST:event_okBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        if (quitMode)
            mainFrame.cleanup();
        else
            setVisible(false);
    }//GEN-LAST:event_cancelBtnActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                OptionsDialog dialog = new OptionsDialog(null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseBtnData;
    private javax.swing.JButton browseBtnExe;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JTextField dataFolderField;
    private javax.swing.JCheckBox defaultBox;
    private javax.swing.JTextField executableFolderField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JCheckBox loadOnStartBox;
    private javax.swing.JButton okBtn;
    private javax.swing.JCheckBox searchPathBox;
    private javax.swing.JCheckBox sortBox;
    // End of variables declaration//GEN-END:variables
    
}
